package com.yygo.webapp.controller;

import com.yygo.model.LoanRequest;
import com.yygo.model.Rule;
import com.yygo.model.vo.LoanRequestVO;
import com.yygo.service.LoanRequestManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * Created by tiansha on 2015/7/8.
 */
@Controller
@RequestMapping("/rule")
public class RuleController {

    @Autowired
    private LoanRequestManager loanRequestManager;

    private MessageSourceAccessor messages;
    @Autowired
    public void setMessages(MessageSource messageSource) {
        messages = new MessageSourceAccessor(messageSource);
    }

    @RequestMapping(value = "/history*", method = RequestMethod.GET)
    @ResponseBody
    public List<LoanRequestVO> handleHistory(@RequestParam(required = true, value = "ruleId") Long ruleId, @RequestParam(required = true, value = "reqId") Long reqId,
                               HttpServletRequest request, Principal principal)
            throws Exception {
        Model model = new ExtendedModelMap();

        List<LoanRequestVO> requestVOList = getLoanRequests(ruleId, reqId);
        model.addAttribute("loanRequestList", requestVOList);

        return requestVOList;
    }

    @RequestMapping(value = "/related*", method = RequestMethod.GET)
    @ResponseBody
    public List<LoanRequestVO> handleRelated(@RequestParam(required = true, value = "ruleId") Long ruleId, @RequestParam(required = true, value = "reqId") Long reqId,
                               HttpServletRequest request, Principal principal)
            throws Exception {
        Model model = new ExtendedModelMap();

        List<LoanRequestVO> requestVOList = getLoanRequests(ruleId, reqId);
        model.addAttribute("loanRequestList", requestVOList);

        return requestVOList;
    }

    public List<LoanRequestVO> getLoanRequests(Long ruleId, Long reqId)
            throws Exception {
        Model model = new ExtendedModelMap();

        LoanRequest loanRequest = loanRequestManager.loadWholeLoanRequest(reqId);
        Rule theRule = null;
        for (Rule rule : loanRequest.getRules()) {
            if (rule.getId().equals(ruleId)) {
                theRule = rule;
                break;
            }
        }

        List<LoanRequestVO> requestVOList = new ArrayList<>();
        if (theRule != null) {
            List<String> numbers = theRule.getNumbers();
            for (String number : numbers) {
                LoanRequest request = loanRequestManager.getLoanRequestByNumber(number);
                requestVOList.add(buildLoanRequest(request, theRule));
            }
        }

       return requestVOList;
    }

    private LoanRequestVO buildLoanRequest(LoanRequest request, Rule rule) {
        LoanRequestVO vo = new LoanRequestVO();
      vo.setId(request.getId());
        vo.setNumber(request.getNumber());
        vo.setRequestDate(request.getRequestDate());
        vo.setName(request.getRequestPerson().getName());
        vo.setDealer(request.getDealer().getName());
        vo.setStatus(messages.getMessage("REQUEST_STATUS_" + request.getStatus().toString(), Locale.SIMPLIFIED_CHINESE));
        vo.setLoanAmount(request.getLoanAmount());
        vo.setVehicle(request.getVehicleInfo());
        vo.setContent(rule.getDescription());

        return vo;
    }
}
